/**
 *  julive
 *
 *  Create by songli on 2021/05/24
 *  Copyright © 2021 居理买房网. All rights reserved.
 */

package client

import "fmt"

var users map[string]*User

var opts = Options{"user", "latest"}

func init() {
	users = make(map[string]*User)
}

type Options struct {
	Name    string
	Version string
}

func New(opt ...Option) (*User, error) {
	for _, o := range opt {
		o(&opts)
	}
	key := fmt.Sprintf("%s-%s", opts.Name, opts.Version)
	userClient, ok := users[key]
	if !ok {
		userClient, err := NewUser(opts.Name, opts.Version)
		if err == nil {
			users[key] = userClient
		}
		return userClient, err
	}
	return userClient, nil
}

type Option func(*Options)

func Name(name string) Option {
	return func(o *Options) {
		o.Name = name
	}
}

func Version(version string) Option {
	return func(o *Options) {
		o.Version = version
	}
}

func Close() {
	for _, client := range users {
		client.Close()
	}
}
